local skels = {}

local shipin = fk.CreateSkill{
    name = "xiaobai__shipin",
}
Fk:loadTranslationTable{
    ["xiaobai__shipin"] = "施贫",
    [":xiaobai__shipin"] = "你使用非伤害牌后，可以令一名体力值或手牌数最低的非当前回合角色摸一张牌，然后可以令其选择一项：1.可以用一张牌替换其未参与的一张延时拼点牌；2.本回合移出游戏。",
    ["#xiaobai__shipin-choose"] = "是否发动 施贫，令一名体力值或手牌数最低的非当前回合角色摸一张牌",
    ["@@xiaobai__shipin-turn"] = "施贫 移出游戏",
    ["#xiaobai__shipin_active"] = "施贫",
    ["#xiaobai__shipin-choice"] = "施贫：用一张牌替换其未参与的一张延时拼点牌，或本回合移出游戏",
}
shipin:addEffect(fk.CardUsing,{
    anim_type = "drawcard",
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(shipin.name) and not data.card.is_damage_card
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room:getAlivePlayers(),function (cp)
            return cp ~= room.current and (table.every(room.alive_players,function (tp)
                return tp.hp >= cp.hp
            end) or table.every(room.alive_players,function (tp)
                return tp:getHandcardNum() >= cp:getHandcardNum()
            end))
        end)
        if #targets > 0 then
            local tos = room:askToChoosePlayers(player,{
                targets = targets,
                max_num = 1,
                min_num = 1,
                skill_name = shipin.name,
                prompt = "#xiaobai__shipin-choose"
            })
            if #tos > 0 then
                event:setCostData(self,{tos = tos})
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        to:drawCards(1,shipin.name)
        local targets = {}
        local pingdians = {}
        local pingdianData = {}
        room.logic:getEventsOfScope(GameEvent.DelayedPindian, 1, function(e)
            local dat = e.data
            if dat.from ~= to and not table.contains(dat.tos,to) then
                local tps = {dat.from.id}
                for _, value in ipairs(dat.tos) do
                    table.insertIfNeed(tps,value.id)
                end
                table.insert(targets, tps)

                local str = (#pingdians+1) .. "." .. Fk:translate(dat.from.general)
                str = str .. "与" .. table.concat(table.map(dat.tos,function (tp)
                    return Fk:translate(tp.general)
                end),"、")
                table.insert(pingdians, str)

                table.insert(pingdianData, dat)
            end
        end, Player.HistoryTurn)
        --to = player
        room:setPlayerMark(to,"xiaobai__shipin_targets-phase",targets)
        room:setPlayerMark(to,"xiaobai__shipin_pingdians-phase",pingdians)
        local dat, result = room:askToUseActiveSkill(to,{
            skill_name = "#xiaobai__shipin_active",
            cancelable = true,
            prompt = "#xiaobai__shipin-choice",
            skip = true
        })
        room:setPlayerMark(to,"xiaobai__shipin_targets-phase",0)
        room:setPlayerMark(to,"xiaobai__shipin_pingdians-phase",0)
        if dat and result then
            local choice = result.interaction
            if choice == "本回合移出游戏" then
                room:setPlayerMark(to, MarkEnum.PlayerRemoved.."-turn", 1)
                room:setPlayerMark(to, "@@xiaobai__shipin-turn", 1)
            elseif #result.targets > 0 and #result.cards > 0 then
                local index = tonumber(string.match(choice,"^(.-)%."))
                local pingdian = pingdianData[index]
                local tp = result.targets[1]
                local card = result.cards[1]
                local cid
                if tp == pingdian.from then
                    cid = pingdian.fromCard:getEffectiveId()
                    pingdian.fromCard = Fk:getCardById(card)
                elseif pingdian.results[tp] and pingdian.results[tp].toCard then
                    cid = pingdian.results[tp].toCard:getEffectiveId()
                    pingdian.results[tp].toCard = Fk:getCardById(card)
                end
                if cid ~= nil then
                    room:moveCards({
                        ids = { card },
                        from = to,
                        toArea = Card.Processing,
                        moveReason = fk.ReasonPut,
                        skillName = shipin.name,
                        moveVisible = false,
                    })
                    room:obtainCard(to,cid,false,fk.ReasonJustMove,to,shipin.name,nil,{to,tp})
                end
            end
        end
    end,
})
shipin:addEffect(fk.PreHpRecover, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:getMark("@@xiaobai__shipin-turn") ~= 0 and data.num ~= 0
    end,
    on_refresh = function(self, event, target, player, data)
        data.num = 0
    end,
})

shipin:addEffect(fk.PreHpLost, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:getMark("@@xiaobai__shipin-turn") ~= 0 and data.num ~= 0
    end,
    on_refresh = function(self, event, target, player, data)
        data.num = 0
    end,
})

shipin:addEffect(fk.DamageInflicted, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:getMark("@@xiaobai__shipin-turn") ~= 0 and data.damage ~= 0
    end,
    on_refresh = function(self, event, target, player, data)
        data.damage = 0
    end,
})

shipin:addEffect("prohibit",{
    prohibit_use = function(self, player, card)
        return player:getMark("@@xiaobai__shipin-turn") ~= 0
    end,
    is_prohibited = function(self, from, to, card)
        return to:getMark("@@xiaobai__shipin-turn") ~= 0
    end,
})
table.insert(skels,shipin)

local shipin_active = fk.CreateSkill{
    name = "#xiaobai__shipin_active",
}
shipin_active:addEffect("active",{
    card_filter = function (self, player, to_select, selected, selected_targets)
        if self.interaction.data and self.interaction.data ~= "本回合移出游戏" then
            return #selected == 0
        else
            return false
        end
    end,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        if self.interaction.data and self.interaction.data ~= "本回合移出游戏" then
            local choices = player:getTableMark("xiaobai__shipin_targets-phase")
            return #selected == 0 and table.contains(choices[tonumber(string.match(self.interaction.data,"^(.-)%."))],to_select.id)
        else
            return false
        end
    end,
    interaction = function (self, player)
        local choices = player:getTableMark("xiaobai__shipin_pingdians-phase")
        table.insert(choices,"本回合移出游戏")
        if #choices > 0 then
            return UI.ComboBox{choices = choices}
        end
    end,
    feasible = function (self, player, selected, selected_cards, card)
        local choice = self.interaction.data
        if choice == "本回合移出游戏" then
            return true
        else
            return #selected_cards == 1 and #selected == 1 and choice
        end
    end,
})
table.insert(skels,shipin_active)

return skels